package cn.flying.cloud.system.role.controller;

import javax.annotation.Resource;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.system.role.entity.SysRoleInfo;
import cn.flying.cloud.system.role.service.SysRoleInfoService;

/**
 * 角色信息表 前端控制器
 *
 * @author: admin
 * @date: 2023年04月19日 16:27
 * @version: 1.0
 */
@RestController
@RequestMapping("/system/role")
public class SysRoleInfoController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private SysRoleInfoService sysRoleInfoService;

    /**
     * 分页查询角色信息表数据
     *
     * @param request
     * @return
     */
    @PostMapping("page")
    public PageResponse<SysRoleInfo> page(@RequestBody PageRequest<SysRoleInfo> request) {
        return sysRoleInfoService.getPageList(request);
    }

    /**
     * 列表查询角色信息表数据
     *
     * @param sysRoleInfo
     * @return
     */
    @PostMapping("list")
    public Rt<?> list(@RequestBody SysRoleInfo sysRoleInfo) {
        List<SysRoleInfo> list = sysRoleInfoService.getList(sysRoleInfo);
        return Rt.success(list);
    }

    /**
     * 获取所有的角色信息表数据
     *
     * @return
     */
    @GetMapping("getAllRoleList")
    public Rt<?> getAllRoleList() {
        List<SysRoleInfo> list = sysRoleInfoService.getAllRoleList();
        return Rt.success(list);
    }

    /**
     * 查询角色信息表详情数据
     *
     * @param sysRoleInfo
     * @return
     */
    @PostMapping("detail")
    public Rt<?> detail(SysRoleInfo sysRoleInfo) {
        return Rt.success(sysRoleInfoService.getOneObj(sysRoleInfo));
    }

    /**
     * 根据主键ID查询角色信息表详情数据
     *
     * @param id
     * @return
     */
    @PostMapping("getById")
    public Rt<?> getById(Long id) {
        SysRoleInfo sysRoleInfo = sysRoleInfoService.getById(id);
        return Rt.success(sysRoleInfo);
    }

    /**
     * 保存角色信息表数据
     *
     * @param sysRoleInfo
     * @return
     */
    @PostMapping("save")
    public Rt<?> save(@RequestBody SysRoleInfo sysRoleInfo) {
        if (Objects.isNull(sysRoleInfo.getId())) {
            // 新增
            sysRoleInfoService.createObj(sysRoleInfo);
        } else {
            // 更新
            sysRoleInfoService.updateObj(sysRoleInfo);
        }
        return Rt.success("保存成功！");
    }

    /**
     * 删除角色信息表数据
     *
     * @param sysRoleInfo
     * @return
     */
    @PostMapping("delete")
    public Rt<?> delete(SysRoleInfo sysRoleInfo) {
        sysRoleInfoService.deleteObj(sysRoleInfo);
        return Rt.success("删除成功！");
    }

    /**
     * 根据Id删除
     *
     * @param id
     * @return
     */
    @GetMapping("deleteById/{id}")
    public Rt<?> deleteById(@PathVariable("id") Long id) {
        sysRoleInfoService.deleteById(id);
        return Rt.success("删除成功！");
    }
}
